How to move an element into another element?
I would like to move one DIV element inside another. For example, I want to move this (including all children):
我想要移動一個div元素去另一個div裡面,例如:
<div id="source">
...
</div>
into this:
放進去這裡面:
<div id="destination">
...
</div>
so that I have this:
然後結果像這樣:
<div id="destination">
<div id="source">
...
</div>
</div>
1.純JS解法
You may want to use the appendTo function (which adds to the end of the element):
你可以用appendto(會加到元素的尾端)
$("#source").appendTo("#destination");
Alternatively you could use the prependTo function (which adds to the beginning of the element):
另一種方式妳可以用prependTo(相反則加到元素的前端)
$("#source").prependTo("#destination");
2.jQuery解法
MOVE:
整個移動
jQuery("#NodesToMove").detach().appendTo('#DestinationContainerNode')
COPY:
複製過去
jQuery("#NodesToMove").appendTo('#DestinationContainerNode')
Note the usage of .detach(). When copying, be careful that you are not duplicating IDs.
注意不要既然是複製就不要用到帶有ID的元素